package com.wise.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * RedisPool配置
 */
public class RedisPool {
    private static JedisPool pool;//jedis连接池
    private static int redis_port = 6379;
    private static int redis_time_out = 5000;
    private static int maxTotal = 20;//最大连接数
    private static int maxIdle = 10;//最大空闲连接数
    private static int minIdle = 5;//最小空闲连接数
    private static boolean testOnBorrow = true;//在取连接时测试连接的可用性
    private static boolean testOnReturn = false;//再还连接时不测试连接的可用性

    static {
        initPool();//初始化连接池
    }

    public static Jedis getJedis(){
        return pool.getResource();
    }

    /**
     * 关闭连接池
     * @param jedis
     */
    public static void close(Jedis jedis){
        jedis.close();
    }

    /**
     * 初始化Jedis连接池
     */
    private static void initPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        config.setBlockWhenExhausted(true);
        pool = new JedisPool(config, "host_address", redis_port,
                                redis_time_out, "password");
    }
}
